var app = getApp();
var c = require('common.js');
var cf = require('config.js');
function login(e, that, info,fun){
  wx.showLoading({
    title: '登录中...',
  })
  // 用户进小程序 首先先获取
  var userinfo=JSON.parse(e.detail.rawData);
  console.log(userinfo);
  wx.login({
    success: res => {
      console.log(res.code);
      c.ajax(
        cf.hostapi + "/Login/codeLogin",
        {
          nickName: userinfo.nickName,
          avatarUrl: userinfo.avatarUrl,
          code: res.code,
        },
        res => {
          if (res.data.status =='success'){
            wx.hideLoading();
            wx.setStorageSync('userinfo', res.data.data);
            that.setData({ hidden: true, userinfo: res.data.data });
            c.pan('登录成功！',1);
            

            iminit('imlogin', res.data.data.id);
            
            if (info =='newslist'){
              that.onShow();
            }
          }else{
            wx.hideLoading();
            c.pan('登录失败！', 0);
          }
        }
      )
    }
    
  })

}
function isLogin() {
  var jim = app.globalData.jimnew;
  
  console.log('isLogin:' + jim.isLogin());

}

function iminit(event, uid){
  
  c.ajax(
    cf.hostapi + 'Im/getLoginInfo', {
    },
    ress => {

      var jim = app.globalData.jimnew;
      console.log('isInit111');
      jim.isInit(function(data){
       console.log(data);
      });
      jim.init({
        "appkey": ress.data.appkey,
        "random_str": ress.data.random_str,
        "signature": ress.data.signature,
        "timestamp": ress.data.timestamp
      }).onSuccess(function (data) {
        console.log(data.code);
        if(data.code==0){
          imevent(event, uid);
          return '返回值';
        };
        console.log('isInit222');
        // jim.isInit();
        // if (event == 'imlogin') {
        //   imlogin(uid, jim);
        // } else {
        //   imregister(uid, jim);
        // }


      }).onFail(function (data) {
        console.log(2222);
        console.log(data);
      });
    }
  )
}



function imevent(event,uid){

  var jim = app.globalData.jimnew;
  if (event == 'imlogin') {

      imlogin(uid, jim);

  } else {
    imregister(uid, jim);
  }
}

function getConversationinfo(){
  var jim = app.globalData.jimnew;
  jim.getConversation().onSuccess(function (data) {
    console.log('getConversationinfo');
    console.log(data.conversations);
    wx.setStorageSync('messageList', data.conversations);
    // console.log('success:getConversation' + JSON.stringify(data));
    // return data;
  }).onFail(function (data) {
    return data;
    // console.log('error:getConversation' + JSON.stringify(data));
  });
}


function onMutiUnreadMsgUpdate(){
  
}

function imlogin(uid,jim){

  jim.login({
    'username': 'cwly' + uid,
    'password': 'cw123456'
  }).onSuccess(function (data) {

    var statusl = jim.isLogin();
   

    // jm.onMutiUnreadMsgUpdate(fn)
    getConversationinfo();
    

      console.log(777888);
      console.log(statusl);
    console.log('success:' + JSON.stringify(data));
    console.log(JSON.stringify(data));

    // isLogin();
    jim.onMsgReceive(function (data) {
      getConversationinfo();
      // console.log(777777777777777777777777);
      // console.log(data);
      isLogin();
      //data = JSON.stringify(data);
      //console.log('1msg_receive:' + data);
      //appendToDashboard('msg_receive:' + data);
    });
    jim.onEventNotification(function (data) {
      console.log('event_receive: ' + JSON.stringify(data));
  
    });

    jim.onSyncConversation(function (data) { //离线消息同步监听
      console.log(data);

    });

    jim.onUserInfUpdate(function (data) {
      console.log('onUserInfUpdate : ' + JSON.stringify(data));

    });

    jim.onSyncEvent(function (data) {
      console.log('onSyncEvent : ' + JSON.stringify(data));

    });

    jim.onMsgReceiptChange(function (data) {
      console.log('onMsgReceiptChange : ' + JSON.stringify(data));


    });

    jim.onSyncMsgReceipt(function (data) {
      console.log('onSyncMsgReceipt : ' + JSON.stringify(data));


    });

    jim.onMutiUnreadMsgUpdate(function (data) {
      console.log('onConversationUpdate : ' + JSON.stringify(data));


    });

    jim.onTransMsgRec(function (data) {
      console.log('onTransMsgRec : ' + JSON.stringify(data));

    });

    jim.onRoomMsg(function (data) {
      console.log('onRoomMsg  : ' + JSON.stringify(data));

    });

    // jim.onSyncConversation(function (data) {
    //   console.log('onSyncConversation');
    //   console.log(data);
    // });

    console.log(111);
    console.log(data);
    //data.code 返回码
    //data.message 描述
  }).onFail(function (data) {
    console.log(222);
    console.log(data.code);
    if (data.code == 880103) {
      imregister(uid,jim);
    }
    // 同上
  });
  // typeof fun == "function" && fun(that)
}



function imregister(uid,jim){
 var userinfo= wx.getStorageSync('userinfo');
  jim.register({
    'username': 'cwly' + uid,
    'password': 'cw123456',
    'nickname': userinfo.username,
    'media_id': userinfo.head,

  }).onSuccess(function (data) {
    imlogin(uid,jim);
    console.log(111);
    console.log(data);
    //data.code 返回码
    //data.message 描述
  }).onFail(function (data) {
    console.log(222);
    console.log(data);
    // 同上
  });
}
module.exports = {
  login: login,
  iminit: iminit,
  isLogin: isLogin,
  imlogin: imlogin,
  getConversationinfo: getConversationinfo
}